﻿using Android.App;
using Android.Util;
using System;
using System.Collections.Generic;
using System.Text;

namespace AndroidApp
{
    public class DensityConst
    {
        /** 默认密度 */
        private float density = 1.0f;
        /** 默认每英寸像素数 */
        private int densityDpi = 160;

        /**
         * 初始化与密度相关的所有变量值
         * 
         * @param activity
         */
        public DensityConst(Activity activity)
        {
            DisplayMetrics dm = new DisplayMetrics();
            //var window = context.GetSystemService(Context.WindowService) as IWindowManager;
            activity.WindowManager.DefaultDisplay.GetMetrics(dm);
            density = dm.Density;
            densityDpi = (int)dm.DensityDpi;
        }

        /**
         * dip转化为像素
         * 
         * @param dip
         * @return
         */
        public int GetPx(int dip)
        {
            return (int)(dip * density);
            // return (int)(dip*(densityDpi/160));

        }

        /**
         * 像素转化为dip
         * 
         * @param px
         * @return
         */
        public int GetDip(int px)
        {
            return (int)(px / density);
            // return (int)((px*160)/densityDpi);
        }
    }
}
